// 获取今天的日期 xxxx-xx-xx
exports.toDay = () => {
	let myDate = new Date();
	const arrMonth = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
	let arrDate = myDate.getDate();
	if (arrDate < 10) {
		arrDate = '0' + arrDate
	}
	let dateStr = myDate.getFullYear() + '-' + arrMonth[myDate.getMonth()] + '-' + arrDate;
	// let toDay = new Date(dateStr); 将字符串转为日期格式
	let toDay = dateStr;
	return toDay
}

// 获取今年年份
exports.toYear = () => {
	let myDate = new Date();
	return myDate.getFullYear()
}

// 拼接年和月 xxxx-xx
exports.yearMonth = (year, month) => {
	if (month < 10) {
		month = '0' + month
	};
	return year + '-' + month;
}

// 将一个时间段拆分成日期数组
exports.getTimeTwo = (start, end) => {
	//初始化日期列表，数组
	let diffdate = new Array();
	let arr = []
	let i = 0;
	//开始日期小于等于结束日期,并循环
	while (start <= end) {
		diffdate[i] = start;
		//获取开始日期时间戳
		let stime_ts = new Date(start).getTime();
		//增加一天时间戳后的日期
		let next_date = stime_ts + (24 * 60 * 60 * 1000);
		//拼接年月日，这里的月份会返回（0-11），所以要+1
		let next_dates_y = new Date(next_date).getFullYear() + '-';
		let next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) +
			'-' : (new Date(next_date).getMonth() + 1) + '-';
		let next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(
			next_date).getDate();
		start = next_dates_y + next_dates_m + next_dates_d;
		//增加数组key
		i++;
	}
	return diffdate;
}

// 时间格式化
exports.jsDateFormatter = (date) => {
	var seperator1 = "-";
	var seperator2 = ":";
	var month = date.getMonth() + 1;
	var strDate = date.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() +
		seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
	return currentdate;
}

// 获取所属考勤时间
exports.salary_date = (init_day) => {
	// 定义拼接年月的函数
	function yearMonth(year, month) {
		if (month < 10) {
			month = '0' + month
		};
		return year + '-' + month;
	};
	// 获取现在的年月 xxxx-xx
	let dateObj = new Date();
	const arrMonth = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
	// 本年本月
	const dateStr = yearMonth(dateObj.getFullYear(), arrMonth[dateObj.getMonth()]);
	// 本年本月的上一月
	let dateYear = dateObj.getFullYear();
	let lastMonth = dateObj.getMonth() - 1;
	if (lastMonth < 0) {
		lastMonth = 11;
		dateYear - 1;
	};
	const lastDateStr = yearMonth(dateYear, arrMonth[lastMonth]);
	if (dateObj.getDate() >= init_day) {
		return dateStr
	} else {
		return lastDateStr
	}
}

// 计算两个日期之间的天数
exports.exdateDiff = (firstDate, secondDate) => {
	var firstDate = new Date(firstDate);
	var secondDate = new Date(secondDate);
	var diff = Math.abs(firstDate.getTime() - secondDate.getTime())
	var result = parseInt(diff / (1000 * 60 * 60 * 24)) + 1;
	return result
}

// 根据出生日期计算年龄 1995-12-08
exports.getAge = (strAge) => {
	var birArr = strAge.split("-");
	var birYear = birArr[0];
	var birMonth = birArr[1];
	var birDay = birArr[2];

	d = new Date();
	var nowYear = d.getFullYear();
	var nowMonth = d.getMonth() + 1; //记得加1
	var nowDay = d.getDate();
	var returnAge;

	if (birArr == null) {
		return false
	};
	var d = new Date(birYear, birMonth - 1, birDay);
	if (d.getFullYear() == birYear && (d.getMonth() + 1) == birMonth && d.getDate() == birDay) {
		if (nowYear == birYear) {
			returnAge = 0; // 
		} else {
			var ageDiff = nowYear - birYear; // 
			if (ageDiff > 0) {
				if (nowMonth == birMonth) {
					var dayDiff = nowDay - birDay; // 
					if (dayDiff < 0) {
						returnAge = ageDiff - 1;
					} else {
						returnAge = ageDiff;
					}
				} else {
					var monthDiff = nowMonth - birMonth; // 
					if (monthDiff < 0) {
						returnAge = ageDiff - 1;
					} else {
						returnAge = ageDiff;
					}
				}
			} else {
				return "出生日期晚于今天，数据有误"; //返回-1 表示出生日期输入错误 晚于今天
			}
		}
		return returnAge;
	} else {
		return ("输入的日期格式错误！");
	}
}

// 判断本轮考勤一共多少天 2022-08
exports.getMonthDay = (dateStr) => {
	const arrNonleapMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 30];
	const arrLeapMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 30];
	if (parseFloat(dateStr.slice(0, 4)) % 4 == 0) {
		return arrLeapMonthDays[parseFloat(dateStr.slice(5, 7) - 1)];
	} else {
		return arrNonleapMonthDays[parseFloat(dateStr.slice(5, 7) - 1)];
	}
}